<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class setting_model extends CI_Model {
	function __construct(){
		parent::__construct();
		$this->load->helper(array('directory','file'));
	}
	
	function select_code_master($id){
		$this->db->select("*");
		$this->db->where('master_id', $id);
		$query = $this->db->get('ch_code_master');
		$row = $query->row_array();
		if($row){
			$data['dbjob'] = 'u';
			$data['master_id'] = $row['master_id'];
			$data['title'] = $row['title'];
			$data['open_yn'] = $row['open_yn'];
			$data['etc1'] = $row['etc1'];
			$data['etc2'] = $row['etc2'];
			$data['etc3'] = $row['etc3'];
		}else{
			$data['dbjob'] = 'i';
			$data['master_id'] = '';
			$data['title'] = '';
			$data['open_yn'] = 'Y';
			$data['etc1'] = '';
			$data['etc2'] = '';
			$data['etc3'] = '';
		}
		return $data;
	}
	
	/**
	 * 마스터 코드 db처리
	 *
	 * @access	public
	 * @return	result
	 */
	function master_code_dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$master_id = $this->input->post('master_id');
		switch($dbjob){
			case('i'):
				$this->db->insert('ch_code_master', $data);
				break;
			case('u'):
				$this->db->where('master_id',$master_id);
				$this->db->update('ch_code_master',$data);
				break;
			case('d'):
				$this->db->where('master_id', $master_id);
				$this->db->update('ch_code_master',$data);
				$this->db->update('ch_code_child',$data);
			break;
		}
		
	}
	
	/**
	 * 마스터 코드 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function list_code_master(){
		$this->db->where('del_yn','N');
		$this->db->order_by('master_id');
		$query = $this->db->get('ch_code_master');
		//echo $this->db->last_query();
		return $query->result();
	}

	/**
	 * 차일드 코드 db처리
	 *
	 * @access	public
	 * @return	void
	 */
	function child_code_dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$code_id = $this->input->post('code_id');
		switch($dbjob){
			case('i'):
				$this->db->select_max('sort');
				$query = $this->db->get('ch_code_child');
				$row = $query->row_array();
				$sort = is_null($row['sort']) ? 1 : $row['sort']+1; 
				$data = array_merge($data,array('sort'=> $sort));
				
				$this->db->insert('ch_code_child', $data);
			break;
			case('u'):
				$this->db->where('code_id',$code_id);
				$this->db->update('ch_code_child',$data);
			break;
			case('d'):
				$this->db->where('code_id', $code_id);
				$this->db->update('ch_code_child',$data);
			break;
			case('asc'): //올림
				$this->db->where('code_id ', $code_id);
				$query = $this->db->get('ch_code_child');
				$row = $query->row_array();
				
				if($row){
					$sort1 = $row['sort'];
					$code_id1 =  $code_id;
					$this->db->where('sort < ', $sort1);
					$this->db->order_by('sort DESC');
					$query = $this->db->get('ch_code_child',1);
					$row = $query->row_array();
					
					if($row){
						$sort2 = $row['sort'];
						$code_id2 = $row['code_id'];
						
						$this->db->where('code_id', $code_id1);
						$this->db->update('ch_code_child',array('sort'=> $sort2));
						
						$this->db->where('code_id', $code_id2);
						$this->db->update('ch_code_child',array('sort'=> $sort1));
					}
				}
			break;
			case('desc'): //내림
				$this->db->where('code_id ', $code_id);
				$query = $this->db->get('ch_code_child');
				$row = $query->row_array();
				
				if($row){
					$sort1 = $row['sort'];
					$code_id1 =  $code_id;
					$this->db->where('sort > ', $sort1);
					$this->db->order_by('sort ASC');
					$query = $this->db->get('ch_code_child',1);
					$row = $query->row_array();
					
					if($row){
						$sort2 = $row['sort'];
						$code_id2 = $row['code_id'];
						
						$this->db->where('code_id', $code_id1);
						$this->db->update('ch_code_child',array('sort'=> $sort2));
						
						$this->db->where('code_id', $code_id2);
						$this->db->update('ch_code_child',array('sort'=> $sort1));
					}
				}
			break;
		}
	}
	
	/**
	 * 차일드 코드 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function list_code_child(){
		$master_id = $this->input->get('master_id');
		$this->db->where(array('master_id' => $master_id,'del_yn'=>'N'));
		$this->db->order_by('sort');
		echo $this->db->last_query();
		$query = $this->db->get('ch_code_child');
		return $query->result();
	}
	
	/**
	 * 차일드 코드 정보
	 *
	 * @access	public
	 * @return	result
	 */
	function select_code_child($id){
		$this->db->select("*");
		$this->db->where('code_id', $id);
		$query = $this->db->get('ch_code_child');
		$row = $query->row_array();
		if($row){
			$data['dbjob'] = 'u';
			$data['code_id'] = $id;
			$data['master_id'] = $row['master_id'];
			$data['title'] = $row['title'];
			$data['open_yn'] = $row['open_yn'];
			$data['etc1'] = $row['etc1'];
			$data['etc2'] = $row['etc2'];
			$data['etc3'] = $row['etc3'];
		}else{
			$data['dbjob'] = 'i';
			$data['code_id'] = '';
			$data['master_id'] = '';
			$data['title'] = '';
			$data['open_yn'] = 'Y';
			$data['etc1'] = '';
			$data['etc2'] = '';
			$data['etc3'] = '';
		}
		return $data;
	}
	
	/**
	 * 사이트 정보
	 *
	 * @access	public
	 * @return	result
	 */
	function select_siteinfo(){
		$this->db->select("*");
		//$this->db->where('id', $id);
		$query = $this->db->get('ch_site_info',1,0);
		//echo $this->db->last_query();
		$row = $query->row_array();
		
		if($row){
			$data['dbjob'] = 'u';
			$data['zipcode1'] = substr($row['post'],0,3);
			$data['zipcode2'] = substr($row['post'],-3);
			$data['addr1'] = $row['addr1'];
			$data['addr2'] = $row['addr2'];
			
			$arrTel = explode('-',$row['tel']);
			$data['tel1'] = $arrTel[0];
			$data['tel2'] = $arrTel[1];
			$data['tel3'] = $arrTel[2];
			
			$arrFax = explode('-',$row['fax']);
			$data['fax1'] = $arrFax[0];
			$data['fax2'] = $arrFax[1];
			$data['fax3'] = $arrFax[2];
			
			$data['admin_name'] = $row['admin_name'];
			
			$arrMail = explode('@',$row['admin_mail']);
			$data['mail_id'] = $arrMail[0];
			$data['mail_domain'] = $arrMail[1];
			
			$data['minister'] = $row['minister'];
			$data['photo'] = $row['photo'];
		}else{
			$data['dbjob'] = 'i';
			$data['zipcode1'] = '';
			$data['zipcode2'] = '';
			$data['addr1'] = '';
			$data['addr2'] = '';
			$data['tel1'] = '';
			$data['tel2'] = '';
			$data['tel3'] = '';
			$data['fax1'] = '';
			$data['fax2'] = '';
			$data['fax3'] = '';
			$data['admin_name'] = '';
			$data['mail_id'] = '';
			$data['mail_domain'] = '';
			$data['minister'] = '';
			$data['photo'] = '';
		}
		return $data;
	}
	
	/**
	 * 사이트 정보  db처리
	 *
	 * @access	public
	 * @return	void
	 */
	function siteinfo_dbjob($data){
		$dbjob = $this->input->post('dbjob');
		switch($dbjob){
			case('i'):
				$this->db->insert('ch_site_info', $data);
				break;
			case('u'):
				$this->db->update('ch_site_info',$data);
			break;
			case('d'):
				$this->db->delete('ch_site_info',$data);
			break;
		}
	}
	
	/**
	 * 배경음악 정보
	 *
	 * @access	public
	 * @return	result
	 */
	function select_bgm(){
		$this->db->order_by('bgm_id DESC');
		$query = $this->db->get('ch_bgm_info',1);
		
		if($query->num_rows() > 0){
			$row = $query->row_array();
			$data['dbjob'] = 'u';
			$data['bgm_id'] = $row['bgm_id'];
			$data['open_yn'] = $row['open_yn'];
			$data['start_date'] = substr($row['start_date'],0,10);
			$data['end_date'] = substr($row['end_date'],0,10);
			$data['bgm_info'] = $row['bgm_info'];
		}else{
			$data['dbjob'] = 'i';
			$data['bgm_id'] = null;
			$data['open_yn'] = 'N';
			$data['start_date'] = null;
			$data['end_date'] = null;
			$data['bgm_info'] = null;
		}
		return $data;
	}
	
	/**
	 * 배경음악 정보  db처리
	 *
	 * @access	public
	 * @return	void
	 */
	function bgminfo_dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		switch($dbjob){
			case('i'):
				$this->db->insert('ch_bgm_info', $data);
			break;
			case('u'):
				$this->db->where('bgm_id', $id);
				$this->db->update('ch_bgm_info',$data);
			break;
			case('d'):
				$this->db->delete('ch_bgm_info',$data);
			break;
		}
		
		echo $this->db->last_query();
		exit;
	}
	
	/**
	 * 현재 사용중인 테마 정보
	 *
	 * @access	public
	 * @return	void
	 */
	function getDesignDefault(){
		$dic = directory_map('./theme',1);
		$data = null;
		foreach($dic as $item){
			$xml = read_file('./theme/'.$item.'/template.xml');
			$parser = new XMLParser($xml);
			$parser->Parse();
	
			if($parser->document->default[0]->tagData =='Y'){
				$data['theme'] = $parser->document->code[0]->tagData;
				break;
			}
		}
		return $data;
	}
	
}
?>